package controllers

import java.io.File

import dao._
import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}
import play.api.routing.JavaScriptReverseRouter
import tool.FormTool
import utils.Utils
import implicits.Implicits._
import play.api.libs.json.Json

import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by Administrator on 2019/8/7
 */
class AppController @Inject()(cc: ControllerComponents, formTool: FormTool, accountDao: AccountDao,
                              userDao: UserDao) extends
  AbstractController(cc) {

  def loginBefore = Action { implicit request =>
    Ok(views.html.login())
  }

  def login = Action.async { implicit request =>
    val data = formTool.loginUserForm.bindFromRequest().get
    accountDao.selectById1.zip(userDao.select(data.name, data.password)).map { case (account, optionUser) =>
      if (data.name == account.account && data.password == account.password) {
        Redirect(routes.AdminController.userManageBefore()).addingToSession("admin" -> data.name)
      } else if (optionUser.isDefined) {
        val user = optionUser.get
        Redirect(routes.UserController.missionManageBefore()).addingToSession("user" -> data.name,
          "id" -> user.id.toString)
      } else {
        Redirect(routes.AppController.loginBefore()).flashing("info" -> "用户名或密码错误!")
      }
    }
  }

  def getProjectSpace = Action { implicit request =>
    val file = new File(Utils.path)
    val canUse = file.getFreeSpace.toDouble.getGb
    val total = file.getTotalSpace.toDouble.getGb
    val used=total-canUse
    Ok(Json.obj("used" -> used, "total" -> total))
  }

  def javascriptRoutes = Action { implicit request =>
    Ok(
      JavaScriptReverseRouter("jsRoutes")(
        controllers.routes.javascript.AppController.getProjectSpace,

        controllers.routes.javascript.MissionController.missionNameCheck,
        controllers.routes.javascript.MissionController.downloadResult,
        controllers.routes.javascript.MissionController.reRunMissionBefore,
        controllers.routes.javascript.MissionController.getAllSuccessMissionNames,
        controllers.routes.javascript.MissionController.newMission,
        controllers.routes.javascript.MissionController.reRunMission,
        controllers.routes.javascript.MissionController.getConfig,
        controllers.routes.javascript.MissionController.reportView,
        controllers.routes.javascript.MissionController.pdfView,
        controllers.routes.javascript.MissionController.downloadData,
        controllers.routes.javascript.MissionController.newMissionBefore,
        controllers.routes.javascript.MissionController.getCpuNum,
        controllers.routes.javascript.MissionController.updateName,
        controllers.routes.javascript.MissionController.getAllMission,
        controllers.routes.javascript.MissionController.getMissionById,
        controllers.routes.javascript.MissionController.reportEnView,

        controllers.routes.javascript.UserController.missionManageBefore,
        controllers.routes.javascript.UserController.getUserById,
        controllers.routes.javascript.UserController.updateInfo,

        controllers.routes.javascript.AdminController.updateMetaboDb,
        controllers.routes.javascript.AdminController.getAllMetaboDb,
        controllers.routes.javascript.AdminController.getAllUser,
        controllers.routes.javascript.AdminController.getUserById,
        controllers.routes.javascript.AdminController.updateUser,

        controllers.routes.javascript.ConfigController.nameCheck,
        controllers.routes.javascript.ConfigController.add,
        controllers.routes.javascript.ConfigController.getAllNames,
        controllers.routes.javascript.ConfigController.getConfig,
        controllers.routes.javascript.ConfigController.getAll,
        controllers.routes.javascript.ConfigController.deleteById,
        controllers.routes.javascript.ConfigController.downloadConfig,
        controllers.routes.javascript.ConfigController.addByFile,

        controllers.routes.javascript.MetaboDbController.add,
        controllers.routes.javascript.MetaboDbController.getAllMetaboDb,
        controllers.routes.javascript.MetaboDbController.nameCheck,
        controllers.routes.javascript.MetaboDbController.deleteById,
        controllers.routes.javascript.MetaboDbController.updateName,
        controllers.routes.javascript.MetaboDbController.detailInfoBefore,
        controllers.routes.javascript.MetaboDbController.getAllUserKeggInfo,
        controllers.routes.javascript.MetaboDbController.getMetaboDbById,

        controllers.routes.javascript.ToolController.getTestPlatform,

      )
    ).as("text/javascript")

  }


}
